<!-- ----------------------------
--  @author: 野狼上校           --
--  @create: 2024-03-24 14:46  --
--  @version: 1.0              --
-------------------------------->
<!-- 资源相关的数据展示及操作 -->

<template>
  <el-breadcrumb :separator-icon="DArrowRight" style='font: normal bold 1.1rem "Fira Sans", serif;'>
    <el-breadcrumb-item v-for="(item, index) in items" :key="index">{{ item }}</el-breadcrumb-item>
  </el-breadcrumb>
  <el-divider/>
  <!--  查询条件  -->
  <el-row :gutter="20" class="my-query">
    <el-col :span="6">
    </el-col>
    <el-col :span="8">
      <el-input placeholder="请输入资源名称" v-model="queryForm.resourceName" clearable/>
    </el-col>
    <el-col :span="4">
      <el-button type="primary" :icon="Search" @click="initResourcesData">查询</el-button>
    </el-col>
    <el-col :span="6">
    </el-col>
  </el-row>

  <!-- 列表数据 -->
  <el-row :gutter="20" class="my-table">
    <el-col :span="4">
      资源列表
    </el-col>
    <el-col :span="16">
    </el-col>
    <el-col :span="4">
      <el-button type="success" :icon="Plus" @click="addRoleHandler">新增资源</el-button>
    </el-col>
  </el-row>

  <!--  表格数据  -->
  <el-table :data="resources" stripe border style="width: 100%">
    <el-table-column prop="resourceName" label="资源名称" width="200" align="center"/>
    <el-table-column prop="resourceUri" label="资源URI" width="200" align="center"/>
    <el-table-column prop="createTime" label="创建时间" width="160" align="center"/>


    <el-table-column prop="action" label="操作" min-width="300px" fixed="right" align="center">
      <template v-slot="scope">
        <!--     修改     -->
        <el-button type="primary" :icon="Edit" @click="editResourceHandler(scope.row)">修改</el-button>
        <!--     删除     -->
        <el-button type="primary" :icon="Delete" @click="deleteResourceHandler(scope.$index,scope.row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

  <!--  分页插件  -->
  <el-pagination
      v-model:current-page="pagination.pageNum"
      v-model:page-size="pagination.pageSize"
      class="el-pagination"
      :page-sizes="[10, 20, 30, 50]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
  />

  <!-- 新增 -->
  <AddResource v-model="dialogAddResourceVisible" :dialogAddResourceVisible="dialogAddResourceVisible"
           @initResourcesData="initResourcesData"/>
  <!-- 修改 -->
  <EditResource v-model="dialogEditResourceVisible" :dialogEditResourceVisible="dialogEditResourceVisible"
            :resourceRowInfo="resourceRowInfo" @initResourcesData="initResourcesData"/>
</template>

<script setup lang="ts">
import {useRoute} from "vue-router";
import {DArrowRight, Delete, Edit, Plus, Search} from "@element-plus/icons-vue";
import {onMounted, ref} from "vue";
import {Action, ElMessage, ElMessageBox} from "element-plus";
import {SUCCESS} from "@/utils/constants.ts";
import {RespResult} from "@/api";
import EditResource from "@/views/system/resource/edit.vue";
import AddResource from "@/views/system/resource/add.vue";
import {deleteResourceById, getResourcesByPage} from "@/api/auth/resource.ts";

const breadcrumb: any = useRoute().meta.title;
const items: string[] = breadcrumb.split(",");

/*查询条件*/
const queryForm = ref({
  resourceName: ""
});

/*表格数据*/
const resources = ref([]);

/*分页条件*/
const pagination = ref({
  pageNum: 1,
  pageSize: 10
})
/*总记录数*/
const total = ref(0);
/*改变每页显示数量*/
const handleSizeChange = (pageSize: number) => {
  pagination.value.pageNum = 1;
  pagination.value.pageSize = pageSize
  initResourcesData();
}
/*改变页码*/
const handleCurrentChange = (pageNum: number) => {
  pagination.value.pageNum = pageNum;
  initResourcesData();
}

/*初始化表格数据*/
onMounted(() => {
  initResourcesData();
})
const initResourcesData = async () => {
  //发送请求
  await getResourcesByPage({
    pageSize: pagination.value.pageSize,
    pageNum: pagination.value.pageNum,
    resourceName: queryForm.value.resourceName
  }).then(
      (response: RespResult) => {
        if (response.code === SUCCESS) {
          const jsonObject = JSON.parse(response.data);
          resources.value = jsonObject.records;
          total.value = jsonObject.total;
        } else {
          ElMessage({
            message: "获取数据失败",
            type: 'error'
          })
        }
      }
  )
}

/*新增*/
const dialogAddResourceVisible = ref(false);
const addRoleHandler = () => {
  //显示弹出框
  dialogAddResourceVisible.value = true;
}

/*修改*/
const dialogEditResourceVisible = ref(false);
const resourceRowInfo = ref({})
const editResourceHandler = (row: any) => {
  //显示弹出框
  dialogEditResourceVisible.value = true;
  resourceRowInfo.value = row;
}


/*删除*/
const deleteResourceHandler = (index: any, id: number) => {
  ElMessageBox.confirm(
      '你确定要删除吗?',
      '警告:',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  ).then(async () => {
    await deleteResourceById({id: id}).then(
        (response: RespResult) => {
          if (response.code === SUCCESS) {
            //从页面上删除记录
            resources.value.splice(index, 1);
            initResourcesData();
            ElMessage({
              message: "删除成功",
              type: "success"
            })
          } else {
            ElMessage({
              message: "删除失败",
              type: "error"
            })
          }
        }
    )
  }).catch((action: Action) => {
    console.log("取消或关闭" + action)
  })
}
</script>

<style scoped>
.my-query {
  margin-left: 0 !important;
  margin-right: 0 !important;
  margin-bottom: 20px;
  padding-top: 15px;
  padding-bottom: 15px;
  border: 1px solid #DDDDDD;
  box-sizing: border-box;
}

.my-table {
  margin-left: 0 !important;
  margin-right: 0 !important;
  margin-bottom: 5px;
  padding-top: 15px;
  padding-bottom: 15px;
  border: 1px solid #DDDDDD;
  box-sizing: border-box;
}

.el-pagination {
  float: right;
  padding: 20px;
  box-sizing: border-box;
}

</style>